/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef SPIMasterTest_H_
#define SPIMasterTest_H_

#include "TestCase.h"
#include "Coms/SPI/SPIMaster.h"
#include <stdio.h>
#include <math.h>

using Coms::SPI::SPIMaster;
using Coms::SPI::SPIHardware;

#define READ_MARGIN 10

class SPIMasterTest : public TestCase<SPIMasterTest>
{
public:

	SPIMasterTest(const prog_mem * testMethodName_P, TestCase<SPIMasterTest>::TestMethod testMethod) :
	TestCase<SPIMasterTest>(testMethodName_P, testMethod)
	{

//		DDRB = (1<<PINB0) | (1<<PINB1) | (1<<PINB2); // set SCK and MOSI as output

		spiHardware = SPIHardware::getSPIHardware(0);
		spiHardware->setPrescaler(Coms::SPI::Prescalers::DIV_128, false);
		spiHardware->setSPIPinsDirections(OUTPUT, OUTPUT, OUTPUT, OUTPUT);
		spiHardware->setMaster();
		spiHardware->transmitMSBFirst();
		spiHardware->enable();


		spiMaster = new SPIMaster(spiHardware, &PORTC, &DDRC, PINC0);

	}

	void setUp()
	{
	}

	void tearDown()
	{
		spiHardware->disable();
	}

	bool testSCA3000()
	{
		while(true)
		{

		uint8_t data[] = {0x09<<2,0,0,0,0,0,0};

		Buffer<uint8_t> transmitAndReceiveBuffer(7, data);

		spiMaster->transmitAndReceive(transmitAndReceiveBuffer);

		int16_t z = data[1] << 8;

		z += data[2] & 0xF8;

		printf("z = %i  status = %x    \r", 3*z/32, data[0]);

		delay(50);
		}



//		return result;

	}

	SPIMaster * spiMaster;
	SPIHardware * spiHardware;

};

#endif /* SPIMasterTest_H_ */
